package com.zhouwei;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        ArrayList<Student> stuArray = new ArrayList<>();
        while (true) {
            System.out.println("------------欢迎来到学生管理系统-------------");
            System.out.println("---1 添加学生信息---");
            System.out.println("---2 删除学生信息---");
            System.out.println("---3 修改学生信息---");
            System.out.println("---4 查看所有学生信息---");
            System.out.println("---5 退出---");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String num = sc.nextLine();
            switch (num) {
                case "1":
                    boolean resp = addStudent(stuArray);
                    if (resp == true) {
                        System.out.println("学生信息添加成功");
                    } else {
                        System.out.println("学生信息添加失败，请重新添加");
                    }
                    break;
                case "2":
                    deleteStudent(stuArray);
                    break;
                case "3":
                    updateStudent(stuArray);
                    break;
                case "4":
                    int stuLength = stuArray.size();
                    if (stuLength == 0) {
                        System.out.println("学生信息为空，请添加学生信息");
                        break;
                    } else {
                        System.out.println("已为您查到如下学生信息:");
                        findAllStudent(stuArray);
                    }
                    break;
                case "5":
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }

    public static boolean addStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号：");
        String stu_id = sc.nextLine();
        System.out.println("请输入学生姓名：");
        String stu_name = sc.nextLine();
        System.out.println("请输入学生年龄：");
        String stu_age = sc.nextLine();
        System.out.println("请输入学生居住地：");
        String stu_address = sc.nextLine();
        Student stu = new Student(stu_id, stu_name, stu_age, stu_address);
        boolean resp = array.add(stu);
        return resp;
    }

    public static void deleteStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的学生学号：");
        String stu_id = sc.nextLine();
        int arrSize = array.size();
        int count = 0;
        while (count >= 0) {
            Student stu = array.get(count);
            if (stu.getSid().equals(stu_id)) {
                array.remove(count);
                System.out.println("删除成功");
                break;
            }
            ++count;
            System.out.println(count);
            if (count == arrSize) {
                System.out.println("未查询到学号为" + stu_id + "的信息，删除失败");
                break;
            }
        }
    }

    public static void updateStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生学号：");
        String stu_id = sc.nextLine();
        int arrSize = array.size();
        int count = 0;
        while (count >= 0) {
            Student stu = array.get(count);
            if (stu.getSid().equals(stu_id)) {
                System.out.println("请输入学生姓名：");
                String stu_name = sc.nextLine();
                System.out.println("请输入学生年龄：");
                String stu_age = sc.nextLine();
                System.out.println("请输入学生居住地：");
                String stu_address = sc.nextLine();

                stu.setName(stu_name);
                stu.setAge(stu_age);
                stu.setAddress(stu_address);
                break;
            }
            ++count;
            System.out.println(count);
            if (count == arrSize) {
                System.out.println("未查询到学号为" + stu_id + "的信息，修改失败");
                break;
            }
        }
    }

    public static void findAllStudent(ArrayList<Student> array) {
        System.out.println("学号\t姓名\t年龄\t居住地");
        for (int i = 0; i < array.size(); i++) {
            Student stu = array.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
        }
    }
}
